<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
<#include '/head.ftl'/>
</head>
<body>
<table id="dg" class="easyui-datagrid" title="用户管理"
       data-options="singleSelect:true,
			fitColumns:true,
			pagination:true,
			toolbar: '#tb',
			url:'/user/list',
			method:'get'">
    <thead>
    <tr>
        <th data-options="field:'id',width:80">ID</th>
        <th data-options="field:'loginName',width:100">loginName</th>
        <th data-options="field:'name',width:80">name</th>
        <th data-options="field:'password',width:80">password</th>
        <th data-options="field:'status',width:50,formatter:Common.formatStatus">status</th>
        <th data-options="field:'createTime',width:160,formatter:Common.formatDate">createTime</th>
        <th data-options="field:'updateTime',width:160,formatter:Common.formatDate">updateTime</th>
        <th data-options="field:'memo',width:100">memo</th>
    </tr>
    </thead>
</table>
<div id="tb">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New
        User</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit
        User</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">Remove
        User</a>
</div>
<div id="dlg" class="easyui-dialog" style="width:400px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
        <div style="margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc">User Information</div>
        <div style="margin-bottom:10px">
            <input name="loginName" class="easyui-textbox" required="true" label="账号:" style="width:100%">
        </div>
        <div style="margin-bottom:10px">
            <input name="name" class="easyui-textbox" required="true" label="姓名:" style="width:100%">
        </div>
        <div style="margin-bottom:10px">
            <input name="password" class="easyui-textbox" required="true" label="密码:" style="width:100%">
        </div>
        <div style="margin-bottom:10px">
            <input class="easyui-combobox" required="true" name="roleId" style="width:100%;" data-options="
                    url:'/role/select',
                    method:'get',
                    valueField:'id',
                    textField:'name',
                    <#--value:[1],-->
                    multiple:false,
                    panelHeight:'auto',
                    label: '角色:',
                    labelPosition: 'left',
                    <#--editable：false-->
                    ">
        </div>
        <div style="margin-bottom:10px">
            <input name="memo" class="easyui-textbox" label="备注:" style="width:100%">
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()" style="width:90px">Save</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
       onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
</div>
<script type="text/javascript">
    var url;
    function newUser() {
        $('#dlg').dialog('open').dialog('center').dialog('setTitle', 'New User');
        $('#fm').form('clear');
        url = '/user/save';
    }
    function editUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', 'Edit User');
            $('#fm').form('load', row);
            url = '/user/update/' + row.id;
        }
    }
    function saveUser() {
        $('#fm').form('submit', {
            url: url,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                var result = eval('(' + result + ')');
                if (result.errorMsg) {
                    $.messager.show({
                        title: 'Error',
                        msg: result.errorMsg
                    });
                } else {
                    $('#dlg').dialog('close');        // close the dialog
                    $('#dg').datagrid('reload');    // reload the user data
                }
            }
        });
    }
    function removeUser() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('Confirm', 'Are you sure you want to destroy this user?', function (r) {
                if (r) {
                    $.post('/user/delete/' + row.id, {id: row.id}, function (result) {
                        if (result.success) {
                            $('#dg').datagrid('reload');    // reload the user data
                        } else {
                            $.messager.show({    // show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    }, 'json');
                }
            });
        }
    }
</script>
</body>
</html>